package com.github.permissiondog.cancanpandroid.ui.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.github.permissiondog.cancanpandroid.R;
import com.github.permissiondog.cancanpandroid.databinding.ActivityApplicationApproveBinding;
import com.github.permissiondog.cancanpandroid.repository.impl.ApplicationRepositoryImpl;
import com.github.permissiondog.cancanpandroid.util.Constants;
import com.github.permissiondog.cancanpandroid.util.NetUtil;

public class ApplicationApproveActivity extends AppCompatActivity {

    private int applicationId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        applicationId = getIntent().getIntExtra("applicationId", -1);
        Log.d("ApplicationApprove", "applicationId: " + applicationId);

        ActivityApplicationApproveBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_application_approve);
        ApplicationRepositoryImpl.getInstance().getApplicationInfo(applicationId, appInfo -> {
            binding.tvCourseName.setText(appInfo.getTemplate().getTableName());
            appInfo.getQuestions().forEach(question -> {
                switch (question.getQuestion().getTitle()) {
                    case "申请原因" -> {
                        if (question.getAnswer() != null)
                            binding.tvReason.setText(question.getAnswer().getAnswer());
                    }
                    case "上传证明" -> {
                        if (question.getAnswer() != null)
                            NetUtil.PICASSO
                                .load(Constants.BASE_URL + "resource/" + question.getAnswer().getAnswer())
                                .into(binding.ivImageUploaded);
                    }
                }
            });
            if (appInfo.isCanApprove()) {
                binding.etComment.setVisibility(View.VISIBLE);
                binding.tvComment.setVisibility(View.VISIBLE);
                binding.btnAccept.setVisibility(View.VISIBLE);
                binding.btnReject.setVisibility(View.VISIBLE);
            } else {
                binding.etComment.setVisibility(View.GONE);
                binding.tvComment.setVisibility(View.GONE);
                binding.btnAccept.setVisibility(View.GONE);
                binding.btnReject.setVisibility(View.GONE);
            }
        });
        binding.btnAccept.setOnClickListener(l -> ApplicationRepositoryImpl.getInstance()
                .approveApplication(
                        applicationId,
                        true,
                        binding.etComment.getText().toString(), ok -> {
            Toast.makeText(this, "已同意申请", Toast.LENGTH_LONG).show();
            var intent = new Intent();
            intent.setAction(Constants.ACTION_REFRESH_APPLICATIONS);
            sendBroadcast(intent);
            finish();
        }));
        binding.btnReject.setOnClickListener(l -> {
            if (binding.etComment.getText().toString().equals("")) {
                Toast.makeText(this, "拒绝审批请输入原因", Toast.LENGTH_LONG).show();
                return;
            }
            ApplicationRepositoryImpl.getInstance()
                    .approveApplication(
                            applicationId,
                            false,
                            binding.etComment.getText().toString(), ok -> {
                Toast.makeText(this, "已拒绝申请", Toast.LENGTH_LONG).show();
                var intent = new Intent();
                intent.setAction(Constants.ACTION_REFRESH_APPLICATIONS);
                sendBroadcast(intent);
                finish();
            });
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}